﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using DTO;

namespace DAO
{
    public class NguoiDungDAO
    {
        public static List<NguoiDungDTO> layThongTin(String user)
        {
            String sql = "select * from NguoiDung where MaNguoiDung = " + user;
            OleDbConnection con = DataProvider.conectDB();
            OleDbCommand cmd = new OleDbCommand(sql, con);
            OleDbDataReader reader = cmd.ExecuteReader();
            List<NguoiDungDTO> lst = new List<NguoiDungDTO>();
            NguoiDungDTO nd = new NguoiDungDTO();

            while (reader.Read())
            {
                nd.MaNguoiDung = reader.GetString(0);
                nd.TenNguoiDung = reader.GetString(1);
                nd.PassWord = reader.GetString(2);
                nd.FullControl = reader.GetBoolean(3);
                nd.TinhTrangXoa = reader.GetBoolean(4);
                lst.Add(nd);
            }
            con.Close();
            return lst;
        }
        public static NguoiDungDTO DangNhap(String user, String pass)
        {
            String strQuery = String.Format("select * from NguoiDung where MaNguoiDung='{0}' and PassWord ='{1}' and chXoa = 0", user, pass);
            //String sql = "select * from NguoiDung where MaNguoiDung = " + user + ",PassWord = " + pass;
            OleDbConnection con = DataProvider.conectDB();
            OleDbCommand cmd = new OleDbCommand(strQuery, con);
            OleDbDataReader reader = cmd.ExecuteReader();
            DataTable table = new DataTable();
            table.Load(reader);
            reader.Close();
            NguoiDungDTO nd = new NguoiDungDTO();
            if (table == null || table.Rows == null || table.Rows.Count == 0)
                return null;
            nd.MaNguoiDung = table.Rows[0]["MaNguoiDung"].ToString();
            nd.TenNguoiDung = table.Rows[0]["TenNguoiDung"].ToString();
            nd.PassWord = table.Rows[0]["PassWord"].ToString();
            nd.FullControl = bool.Parse(table.Rows[0]["FullControl"].ToString());
            nd.TinhTrangXoa = bool.Parse(table.Rows[0]["chXoa"].ToString());
            con.Close();
            return nd;
        }
        public static Boolean DoiMatKhau(String user, String pass)
        {
            String strQuery = String.Format("Update [NguoiDung] Set [PassWord] = '{0}' Where [MaNguoiDung] = '{1}'", pass.Trim(), user.Trim());
            Boolean flag = DataProvider.ExcuteNonQuery(strQuery);
            return flag;
            //try
            //{
            //    cmd.ExecuteNonQuery();
            //}catch(Exception ex)
            //{
            //    throw ex;
            //    con.Close();
            //    return false;
            //}
            //con.Close();
            //return true;
        }
    }
}
